<template>
  <!-- 把两个事件函数分开 -->
  <!-- <li :class="isActive? 'active' : '' "
    @mouseenter="mouseEnterHandle"
    @mouseleave="mouseLeaveHandle"
  > -->

  <!-- 两个事件逻辑一致,可以使用一个事件函数 -->
  <li
    :class="isActive ? 'active' : '' "
    @mouseenter="mouseChangeHandle('enter')"
    @mouseleave="mouseChangeHandle('leave')"
  >
    <label>
      <!-- props不能修改 所以用checked控制  -->

      <!-- !!!不能在@click里面 调用自定义事件 因为this找不到!!! -->
      <input type="checkbox" :checked="done" @click="checkedHandle"/>
      <span>{{ thing }}</span>
    </label>
    <button class="btn btn-danger" @click="deleteHandle">删除</button>
  </li>
</template>

<script>
export default {
  name: "Item",
  props: ["id", "thing", "done"], //批量拿到对应的数据
  data() {
    return {
      isActive: false,
    };
  },
  methods: {
    //点击切换调用List传递的自定义事件 把id发给List(子传父)
    checkedHandle(){
      this.$emit("asignCheckedHandle" , this.id)
    },

    //点击删除调用List传递的自定义事件 把id发给List(子传父)
    deleteHandle(){
      this.$emit("asignDeleteHandle" , this.id)
    },


    //两个事件函数分开写
    /* mouseEnterHandle() {
      this.isActive = true;
    },
    mouseLeaveHandle() {
      this.isActive = false;
    }, */

    //两个移入溢移出事件函数合并到一起
    mouseChangeHandle(type){
      //如果是移入就传enter 就把true赋值给isActive 移出反之
      this.isActive = type === 'enter'
    }
  },
};
</script>

<style scoped>
/*item*/
li {
  list-style: none;
  height: 36px;
  line-height: 36px;
  padding: 0 5px;
  border-bottom: 1px solid #ddd;
}

li label {
  float: left;
  cursor: pointer;
}

li label li input {
  vertical-align: middle;
  margin-right: 6px;
  position: relative;
  top: -1px;
}

li button {
  float: right;
  display: none;
  margin-top: 3px;
}

li:before {
  content: initial;
}

li:last-child {
  border-bottom: none;
}



li.active {
  background: #ccc;
}

li.active button {
  display: block;
}
</style>